home *** CD-ROM | disk | FTP | other *** search
/ CD ROM Paradise Collection 4 / CD ROM Paradise Collection 4 1995 Nov.iso / program / swagg_m.zip / GRAPHICS.SWG / 0025_Graphic Spinning Disk.pas < prev    next >
Pascal/Delphi Source File  |  1993-08-27  |  3KB  |  107 lines

  1. {
  2. WILLIAM SITCH
  3.  
  4. > Okay, I've just finally got my hands on the formulas for
  5. > doing good Graphics manipulations...well, I decided to start
  6. > With something simple.  A rotating square.  But it DOESN'T
  7. > WORK RIGHT.  I noticed the size seemed to shift in and out
  8. > and a little testing showed me that instead of following a
  9. > circular path (as they SHOULD), the corners (while spinning)
  10. > actually trace out an OCTAGON. Why????  I've checked and
  11. > rechecked the formula logic...It's just as I was given.  So
  12. > there's some quirk about the code that I don't know about.
  13. > Here's the rotating routine:
  14.  
  15. Ahhh... "rounding errors" is what my comp sci teacher explained to me, but
  16. there isn't much you can do about it... I've included my (rather long)
  17. spinning disc code to take a look at ... feel free to try to port it to your
  18. application...
  19.  
  20. }
  21.  
  22. Uses
  23.   Graph, Crt;
  24.  
  25. Procedure spin_disk;
  26. Type
  27.   pointdataType = Array [1..4] of Record x,y : Integer; end;
  28. Const
  29.   delVar = 10;
  30.  
  31. Var
  32.   ch       :  Char;
  33.   p, op    :  pointdataType;
  34.   cx, cy,
  35.   x, y, r  :  Integer;
  36.   i        :  Integer;
  37.   rot      :  Integer;
  38.   tempx,
  39.   tempy    :  Integer;
  40.   theta    :  Real;
  41.   down     :  Boolean;
  42.   del      :  Real;
  43. begin
  44.   cx := getmaxx div 2;
  45.   cy := getmaxy div 2;
  46.   r := 150;
  47.   circle(cx,cy,r);
  48.  
  49.   rot := 0;
  50.   p[1].x := 100;  p[1].y := 0;
  51.   p[2].x := 0;    p[2].y := -100;
  52.   p[3].x := -100; p[3].y := 0;
  53.   p[4].x := 0;    p[4].y := 100;
  54.   del := 50;
  55.   down := True;
  56.  
  57.   Repeat
  58.     rot := rot + 2;
  59.     theta := rot * 3.14 / 180;
  60.     For i := 1 to 4 do
  61.       begin
  62.         tempx := p[i].x;
  63.         tempy := p[i].y;
  64.         op[i].x := p[i].x;
  65.         op[i].y := p[i].y;
  66.         p[i].x := round(cos(theta) * tempx - sin(theta) * tempy);
  67.         p[i].y := round(sin(theta) * tempx + cos(theta) * tempy);
  68.       end;
  69.     setcolor(0);
  70.     line(op[1].x + cx,cy - op[1].y,op[2].x + cx,cy - op[2].y);
  71.     line(op[2].x + cx,cy - op[2].y,op[3].x + cx,cy - op[3].y);
  72.     line(op[3].x + cx,cy - op[3].y,op[4].x + cx,cy - op[4].y);
  73.     line(op[4].x + cx,cy - op[4].y,op[1].x + cx,cy - op[1].y);
  74.     For i := 1 to 4 do
  75.       line(op[i].x + cx,cy - op[i].y,cx,cy);
  76.     setcolor(11);
  77.     line(p[1].x + cx,cy - p[1].y,p[2].x + cx,cy - p[2].y);
  78.     line(p[2].x + cx,cy - p[2].y,p[3].x + cx,cy - p[3].y);
  79.     line(p[3].x + cx,cy - p[3].y,p[4].x + cx,cy - p[4].y);
  80.     line(p[4].x + cx,cy - p[4].y,p[1].x + cx,cy - p[1].y);
  81.     setcolor(10);
  82.     For i := 1 to 4 do
  83.       line(p[i].x + cx,cy - p[i].y,cx,cy);
  84.     if (del < 1) then
  85.       down := False
  86.     else if (del > 50) then
  87.       down := True;
  88.     if (down) then
  89.       del := del - delVar
  90.     else
  91.       del := del + delVar;
  92.     Delay(round(del));
  93.   Until (KeyPressed = True);
  94.   ch := ReadKey;
  95.   NoSound;
  96. end;
  97.  
  98. Var
  99.   Gd, Gm : Integer;
  100.  
  101. begin
  102.   Gd := Detect;
  103.   InitGraph(Gd, Gm, 'd:\bp\bgi');
  104.  
  105.   Spin_disk;
  106.  
  107. end.